<?php
//公共模块
class commonMod
{
public $model;//数据库模型对象

public $tpl;//模板对象
public $config;//全局配置
static $global;//静态变量，用来实现单例模式
public function __construct()
{
	session_start();//开启session

	//参数配置
	if(!isset(self::$global['config']))
	{
		global $config;
		self::$global['config']=$config;
	}
	$this->config=self::$global['config'];//配置

	//数据库模型初始化
	if(!isset(self::$global['model']))
	{
		require(CP_PATH.'core/cpModel.class.php');//加载数据库模型类
		self::$global['model']=new cpModel($this->config);//实例化数据库模型类
	}
	$this->model=self::$global['model'];//数据库模型对象
	 //模板初始化
             if(!isset(self::$global['tpl']))
            {
   		require(CP_PATH.'core/cpTemplate.class.php');//加载模板类
 		self::$global['tpl']=new cpTemplate($this->config);//实例化模板类
   	 }
 		$this->tpl=self::$global['tpl'];//模板类对象

	//语言包目录
	//$config['LANG_ADMIN']='admin/';
	//Lang::init($config);

	//插件模块
	$config['PLUGIN_PATH']='./../plugins/';
	Plugin::init('Admin',$config);

	//登录与权限验证
	$config['AUTH_LOGIN_URL']=__APP__.'/index/login.html';
	$config['AUTH_LOGIN_NO']=array('index'=> array('login','verify'));//无须登录的模块以及操作
	$config['AUTH_POWER_CACHE']=false;
	Auth::check($this->model,$config);//检查是否登录
}


//模板变量解析
protected function assign($name, $value)
{
	return $this->tpl->assign($name, $value);
}

           //模板输出
	protected  function display($tpl='')
	{

		return $this->tpl->display($tpl);
	}

//直接跳转
protected function redirect($url)
{
	header('location:'.$url,false,301);
	exit;
}

//出错之后跳转，后退到前一页
protected function error($msg)
{
	header("Content-type: text/html; charset=utf-8");
	$msg="alert('$msg');";
	echo "<script>$msg history.go(-1);</script>";
	exit;
}
//操作成功之后跳转,默认三秒钟跳转
/*protected function success($msg,$way,$url=NULL,$waitSecond=3)
{
	if($url==NULL)
	$url=__URL__.$way;
	$this->assign('message',$msg);
	$this->assign('url',$url);
	$this->assign('waitSecond',$waitSecond);
	$this->display(substr(dirname(__FILE__),0,strrpos(dirname(__FILE__),'\\')).'/template/success');
	exit;
}*/

	//操作成功之后跳转,默认三秒钟跳转http://127.0.0.5/admin/index.php/user
protected   function success($msg,$way,$url=NULL,$waitSecond=2)
{
		if($url==NULL){
			$url=__URL__.$way;
			//echo __URL__.$way;
		}
		$this->assign('message',$msg);
		//echo "url==".$url;
		$this->assign('url',$url);
		$this->assign('waitSecond',$waitSecond);
		$this->display('success');
		exit;
}
/*
功能:分页
$url，基准网址，若为空，将会自动获取，不建议设置为空
$total，信息总条数
$perpage，每页显示行数
$pagebarnum，分页栏每页显示的页数
$mode，显示风格，参数可为整数1，2，3，4任意一个
*/
protected function page($url,$total,$perpage=10,$pagebarnum=10,$mode=4)
{
	$page=new page();
	return $page->show($url,$total,$perpage,$pagebarnum,$mode);
}
/*文件上传*/
protected function _upload($upload_dir)
{
	$upload = new UploadFile();
	//设置上传文件大小
	$upload->maxSize=1024*1024*2;//最大2M
	//设置上传文件类型
	$upload->allowExts  = explode(',','jpg,gif,png,bmp,php,doc,docx,rar,zip');

	//设置附件上传目录
	$upload->savePath =$upload_dir."/";
	$upload->saveRule = cp_uniqid;

	if(!$upload->upload())
	{
		//捕获上传异常
		$this->error($upload->getErrorMsg());
	}
	else
	{
		//取得成功上传的文件信息
		return $upload->getUploadFileInfo();
	}
}

}
?>